package com.geo.ai.model;

import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
 * AI模型配置类
 * 
 * @author GEO Team
 * @version 1.0
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ModelConfig {
    
    /**
     * 配置ID
     */
    private String id;
    
    /**
     * 模型名称
     */
    private String modelName;
    
    /**
     * 模型提供商
     */
    private String provider;
    
    /**
     * API端点
     */
    private String apiEndpoint;
    
    /**
     * API密钥（加密存储）
     */
    private String apiKey;
    
    /**
     * 是否启用
     */
    private Boolean enabled;
    
    /**
     * 优先级（数字越小优先级越高）
     */
    private Integer priority;
    
    /**
     * 每token成本
     */
    private Double costPerToken;
    
    /**
     * 每分钟请求限制
     */
    private Integer rateLimitRpm;
    
    /**
     * 最大token数
     */
    private Integer maxTokens;
    
    /**
     * 默认温度参数
     */
    private Double defaultTemperature;
    
    /**
     * 超时时间（毫秒）
     */
    private Long timeoutMs;
    
    /**
     * 重试次数
     */
    private Integer retryTimes;
    
    /**
     * 模型参数
     */
    private Map<String, Object> parameters;
    
    /**
     * 支持的功能
     */
    private ModelCapabilities capabilities;
    
    /**
     * 模型能力配置
     */
    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ModelCapabilities {
        /**
         * 是否支持流式输出
         */
        private Boolean supportsStreaming;
        
        /**
         * 是否支持函数调用
         */
        private Boolean supportsFunctionCalling;
        
        /**
         * 是否支持图片输入
         */
        private Boolean supportsImageInput;
        
        /**
         * 是否支持多模态
         */
        private Boolean supportsMultimodal;
        
        /**
         * 支持的语言列表
         */
        private java.util.List<String> supportedLanguages;
        
        /**
         * 上下文窗口大小
         */
        private Integer contextWindow;
        
        /**
         * 最大输出长度
         */
        private Integer maxOutputLength;
        
        // 手动添加builder方法
        public static ModelCapabilitiesBuilder builder() {
            return new ModelCapabilitiesBuilder();
        }
        
        public static class ModelCapabilitiesBuilder {
            private Boolean supportsStreaming;
            private Boolean supportsFunctionCalling;
            private Boolean supportsImageInput;
            private Boolean supportsMultimodal;
            private java.util.List<String> supportedLanguages;
            private Integer contextWindow;
            private Integer maxOutputLength;
            
            public ModelCapabilitiesBuilder supportsStreaming(Boolean supportsStreaming) {
                this.supportsStreaming = supportsStreaming;
                return this;
            }
            
            public ModelCapabilitiesBuilder supportsFunctionCalling(Boolean supportsFunctionCalling) {
                this.supportsFunctionCalling = supportsFunctionCalling;
                return this;
            }
            
            public ModelCapabilitiesBuilder supportsImageInput(Boolean supportsImageInput) {
                this.supportsImageInput = supportsImageInput;
                return this;
            }
            
            public ModelCapabilitiesBuilder supportsMultimodal(Boolean supportsMultimodal) {
                this.supportsMultimodal = supportsMultimodal;
                return this;
            }
            
            public ModelCapabilitiesBuilder supportedLanguages(java.util.List<String> supportedLanguages) {
                this.supportedLanguages = supportedLanguages;
                return this;
            }
            
            public ModelCapabilitiesBuilder contextWindow(Integer contextWindow) {
                this.contextWindow = contextWindow;
                return this;
            }
            
            public ModelCapabilitiesBuilder maxOutputLength(Integer maxOutputLength) {
                this.maxOutputLength = maxOutputLength;
                return this;
            }
            
            public ModelCapabilities build() {
                ModelCapabilities capabilities = new ModelCapabilities();
                capabilities.supportsStreaming = this.supportsStreaming;
                capabilities.supportsFunctionCalling = this.supportsFunctionCalling;
                capabilities.supportsImageInput = this.supportsImageInput;
                capabilities.supportsMultimodal = this.supportsMultimodal;
                capabilities.supportedLanguages = this.supportedLanguages;
                capabilities.contextWindow = this.contextWindow;
                capabilities.maxOutputLength = this.maxOutputLength;
                return capabilities;
            }
        }
    }
    
    /**
     * 验证配置是否有效
     * @return 是否有效
     */
    public boolean isValid() {
        return modelName != null && !modelName.trim().isEmpty() &&
               apiEndpoint != null && !apiEndpoint.trim().isEmpty() &&
               apiKey != null && !apiKey.trim().isEmpty() &&
               enabled != null && enabled;
    }
    
    /**
     * 获取完整的API URL
     * @param path API路径
     * @return 完整URL
     */
    public String getFullApiUrl(String path) {
        if (apiEndpoint.endsWith("/")) {
            return apiEndpoint + path;
        }
        return apiEndpoint + "/" + path;
    }
    
    // 手动添加builder方法以解决编译问题
    public static ModelConfigBuilder builder() {
        return new ModelConfigBuilder();
    }
    
    // 添加缺失的getter方法
    public String getApiKey() {
        return apiKey;
    }
    
    public String getApiEndpoint() {
        return apiEndpoint;
    }
    
    public Double getDefaultTemperature() {
        return defaultTemperature;
    }
    
    public Double getCostPerToken() {
        return costPerToken;
    }
    
    public String getProvider() {
        return provider;
    }
    
    public String getModelName() {
        return modelName;
    }
    
    public Integer getPriority() {
        return priority;
    }
    
    public Boolean getEnabled() {
        return enabled;
    }
    
    public ModelCapabilities getCapabilities() {
        return capabilities;
    }
    
    public static class ModelConfigBuilder {
        private String id;
        private String modelName;
        private String provider;
        private String apiEndpoint;
        private String apiKey;
        private Boolean enabled;
        private Integer priority;
        private Double costPerToken;
        private Integer rateLimitRpm;
        private Integer maxTokens;
        private Double defaultTemperature;
        private Long timeoutMs;
        private Integer retryTimes;
        private Map<String, Object> parameters;
        private ModelCapabilities capabilities;
        
        public ModelConfigBuilder id(String id) {
            this.id = id;
            return this;
        }
        
        public ModelConfigBuilder modelName(String modelName) {
            this.modelName = modelName;
            return this;
        }
        
        public ModelConfigBuilder provider(String provider) {
            this.provider = provider;
            return this;
        }
        
        public ModelConfigBuilder apiEndpoint(String apiEndpoint) {
            this.apiEndpoint = apiEndpoint;
            return this;
        }
        
        public ModelConfigBuilder apiKey(String apiKey) {
            this.apiKey = apiKey;
            return this;
        }
        
        public ModelConfigBuilder enabled(Boolean enabled) {
            this.enabled = enabled;
            return this;
        }
        
        public ModelConfigBuilder priority(Integer priority) {
            this.priority = priority;
            return this;
        }
        
        public ModelConfigBuilder costPerToken(Double costPerToken) {
            this.costPerToken = costPerToken;
            return this;
        }
        
        public ModelConfigBuilder rateLimitRpm(Integer rateLimitRpm) {
            this.rateLimitRpm = rateLimitRpm;
            return this;
        }
        
        public ModelConfigBuilder maxTokens(Integer maxTokens) {
            this.maxTokens = maxTokens;
            return this;
        }
        
        public ModelConfigBuilder defaultTemperature(Double defaultTemperature) {
            this.defaultTemperature = defaultTemperature;
            return this;
        }
        
        public ModelConfigBuilder timeoutMs(Long timeoutMs) {
            this.timeoutMs = timeoutMs;
            return this;
        }
        
        public ModelConfigBuilder retryTimes(Integer retryTimes) {
            this.retryTimes = retryTimes;
            return this;
        }
        
        public ModelConfigBuilder parameters(Map<String, Object> parameters) {
            this.parameters = parameters;
            return this;
        }
        
        public ModelConfigBuilder capabilities(ModelCapabilities capabilities) {
            this.capabilities = capabilities;
            return this;
        }
        
        public ModelConfig build() {
            ModelConfig config = new ModelConfig();
            config.id = this.id;
            config.modelName = this.modelName;
            config.provider = this.provider;
            config.apiEndpoint = this.apiEndpoint;
            config.apiKey = this.apiKey;
            config.enabled = this.enabled;
            config.priority = this.priority;
            config.costPerToken = this.costPerToken;
            config.rateLimitRpm = this.rateLimitRpm;
            config.maxTokens = this.maxTokens;
            config.defaultTemperature = this.defaultTemperature;
            config.timeoutMs = this.timeoutMs;
            config.retryTimes = this.retryTimes;
            config.parameters = this.parameters;
            config.capabilities = this.capabilities;
            return config;
        }
    }
}